/**
 * 
 */
package org.rollinitiative.d20web.charactersheet.client;

import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.PopupPanel;
import com.google.gwt.user.client.ui.ScrollPanel;
import com.google.gwt.user.client.ui.TextArea;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;

/**
 * @author bebopjmm
 * 
 */
public class TalentsView extends Composite
{
    private final VerticalPanel talents = new VerticalPanel();


    public TalentsView()
    {
        // Setup the view
        initView();
        initWidget(talents);
    }


    public void setCharacter(SkillsData skills)
    {
        // Update content
        TalentEntry[] talents = skills.getTalents();
        initView();
        for (TalentEntry talentEntry : talents) {
            addTalent(talentEntry.getTalentName(), talentEntry.getTalentDescription());
        }

    }


    private void initView()
    {
        talents.clear();
        // Create the header label.
        talents.add(new Label("Feats and Talents"));
    }


    private void addTalent(String talentName, final String talentDescript)
    {
        final HTML link = new HTML("<a href='javascript:;'>" + talentName + "</a>", true);

        talents.add(link);

        // Add a click listener that displays a ContactPopup when it is clicked.
        link.addClickListener(new ClickListener() {
            public void onClick(Widget sender)
            {
                TalentPopup popup = new TalentPopup(talentDescript);
                int left = link.getAbsoluteLeft() + 14;
                int top = link.getAbsoluteTop() + 14;
                popup.setPopupPosition(left, top);
                popup.show();
            }
        });
    }

    /**
     * A simple popup that displays a contact's information.
     */
    private class TalentPopup extends PopupPanel
    {
        // TODO investigate HTML based instead of TextArea for richer content.
        public TalentPopup(String detail)
        {
            // The popup's constructor's argument is a boolean specifying that it
            // auto-close itself when the user clicks outside of it.
            super(true);

            ScrollPanel panel = new ScrollPanel();
            // panel.setAlwaysShowScrollBars(true);
            panel.setHeight("100px");
            panel.setWidth("325px");
            TextArea detailText = new TextArea();
            detailText.setText(detail);
            detailText.setHeight("500px");
            detailText.setWidth("300px");
            detailText.setReadOnly(true);
            panel.add(detailText);
            add(panel);
        }
    }
}
